import Vue from "vue"

import Vuex from "vuex"

import { selectAllPlan, selectTodayClock, selectWeeklyClock } from "@/api/index.js"

Vue.use(Vuex)

const store = new Vuex.Store({
		state: {
			todayClick: 0,
			weeklyClick: 0,
			rate: 0,
			allPlan: 0,
			ranking: 0,
			increase: 0,
			myIntrgral: 0
		},
		mutations: {
			async unit(state){
				let user = uni.getStorageSync("users");
				let today = await selectTodayClock({user});
				console.log(today.data,'today.data.length');
				let plan = await selectAllPlan({user});
				console.log(plan.data,'plan.data.length');
				let week = await selectWeeklyClock({user});
				console.log(week,'week');
				state.todayClick = today.data.length
				state.allPlan = plan.data.length
				state.weeklyClick = week.data.length
				// state.increase = this.increaseData(today.data)
				state.increase = this.commit("increaseData",today.data)
				state.myIntrgral = this.commit("increaseData",plan.data)
				// state.myIntrgral = this.increaseData(plan.data)
				setInterval(()=>{
					state.rate = (state.todayClick/state.allPlan*100).toFixed(2)
					state.ranking = (state.increase/state.myIntrgral*100).toFixed(2)
				},1000)
			},
			increaseData(state,data){
				let temp = 0;
				console.log(data,'dataaaaaaaaaa');
				data.forEach(item => {
					temp += item.integral
				})
				return temp
			}
		},
		actions: {}
})


export default store